##### iGraph edge display and analysis
rm(list=ls())
library(stringr)
library(igraph)
library(dplyr)
library(plyr)

### load before and after data
load("replication/data/before.RData")
load("replication/data/after.RData")
load("replication/output/coopDV.RData")

## get rid of NAs
before <- na.omit(before)
after <- na.omit(after)

####get rid of countryA-countryA in edge list 
before <- before[before$SourceCountry != before$TargetCountry, ]

# ### thin list down to only unique combination of dyads
#before <- before[!duplicated(before$simon), ] #THIS MAKES NO SENSE
idmaker = function(vec){
  return(paste(sort(vec), collapse=""))
}

ids = apply(before[,c('SourceCountry', 'TargetCountry')], 1, idmaker)

before = cbind(before, ids)
before = before[!duplicated(before[,c('ids')]), ]

### focus on only edges and pos/neg
beforeComb <- before[ ,c(1,2,7)]
beforeCoop <- before[ ,c(1,2,5)]
beforeConf <- before[ ,c(1,2,6)]


## get rid of zero edges
beforeComb <- subset(beforeComb, simon != 0)
beforeConf <- subset(beforeConf, undirNeg != 0)
beforeCoop <- subset(beforeCoop, undirPos != 0)




# FIGURE 1a ---------------------------------------------------------------


## convert to graph object
beforeComb$SourceCountry[beforeComb$SourceCountry=="United Arab Emirates"] = "UAE"
beforeComb$TargetCountry[beforeComb$TargetCountry=="United Arab Emirates"] = "UAE"

g <- graph.data.frame(beforeComb, directed = F)


summary(g)

# check igraph characteristics
V(g)
E(g)


# color edges based on positve/negative attribute
E(g)$color <- ifelse(E(g)$simon > 0, "lightblue", "#e41a1c")


# change size of nodes, edge, and arrows based on degree
#V(g)$size <- degree(g)/2
E(g)$width <- abs(E(g)$simon)/2
E(g)$arrow.size <- abs(E(g)$simon)*0
V(g)$color = "lightblue"
V(g)$size = 0

# circle layout
order = c(beforeComb$SourceCountry, beforeComb$TargetCountry) %>% unique %>% sort

lay = layout_in_circle(g, order = order)


## Figure 1.a
set.seed(9999)
par(mai=c(0,0,0.2,0)) #streches graph to reduce white space
pdf('replication/paper/figures/beforeSimon_new.pdf')
plot(g,
     layout = lay, # layout style that reduces edge-crossing
     vertex.label.dist=.5, # moves label slightly off vertex for easier read
     vertex.frame.color='black', #vertex coloring (edge)
     vertex.label.color='black', #vertex coloring (inside)
     vertex.label.font=2, #font size of label
     vertex.label.cex=1) # thickness
dev.off()



# FIGURE 1b ---------------------------------------------------------------



####get rid of countryA-countryA in edge list 
after <- after[after$SourceCountry != after$TargetCountry, ]

# ### thin list down to only unique combination of dyads
#after <- after[!duplicated(after$simon), ] #THIS MAKES NO SENSE
idmaker = function(vec){
  return(paste(sort(vec), collapse=""))
}

ids = apply(after[,c('SourceCountry', 'TargetCountry')], 1, idmaker)

after = cbind(after, ids)
after = after[!duplicated(after[,c('ids')]), ]

### focus on only edges and pos/neg
afterComb <- after[ ,c(1,2,7)]
afterCoop <- after[ ,c(1,2,5)]
afterConf <- after[ ,c(1,2,6)]


## get rid of zero edges
afterComb <- subset(afterComb, simon != 0)
afterConf <- subset(afterConf, undirNeg != 0)
afterCoop <- subset(afterCoop, undirPos != 0)


# fix Oman not showing up on graph by adding arbitrary weight

newrow <- c("Oman", "Iran", .0000005)
temp <- rbind(afterComb[1:68, ], newrow, afterComb[-(1:68), ] )


## convert to graph object

temp$SourceCountry[temp$SourceCountry=="United Arab Emirates"] = "UAE"
temp$TargetCountry[temp$TargetCountry=="United Arab Emirates"] = "UAE"

g <- graph.data.frame(temp, directed = T)
summary(g)

# check igraph characteristics
V(g)
E(g)
V(g)$color = "lightblue"

# color edges based on positve/negative attribute
E(g)$color <- ifelse(E(g)$simon > 0, "lightblue", "#e41a1c")

# change size of nodes, edge, and arrows based on degree
E(g)$width <- abs(as.numeric(E(g)$simon))/2
E(g)$arrow.size <- abs(as.numeric(E(g)$simon))*0
V(g)$size = 0


# circle layout
order = c(beforeComb$SourceCountry, beforeComb$TargetCountry) %>% unique %>% sort

lay = layout_in_circle(g, order = order)


# Figure 1.b:

set.seed(9999)
par(mai=c(0,0,.2,0)) 
pdf('replication/paper/figures/afterSimon_new.pdf')
plot(g,
     layout = lay,
     vertex.label.dist=0.5,
     vertex.frame.color='black',
     vertex.label.color='black',
     vertex.label.font=2,
     vertex.label.cex=1)
dev.off()




# FIGURE 4 ---------------------------------------------------------------



FinalData$SourceCountry[FinalData$SourceCountry=="United Arab Emirates"] = "UAE"
FinalData$TargetCountry[FinalData$TargetCountry=="United Arab Emirates"] = "UAE"


g <- graph.data.frame(FinalData[FinalData$ChangeCoop >= .5,], directed = T)
summary(g)
g = g + vertex("Syria")


# color edges based on positve/negative attribute
#E(g)$color <- ifelse(E(g)$ChangeCoop > 0, "lightblue1", "lightcoral")
E(g)$color <- ifelse(E(g)$ChangeCoop > 0, "lightblue1", "red")
V(g)$color = "lightblue1"
V(g)$size = 8

# change size of nodes, edge, and arrows based on degree
#V(g)$size <- 10
E(g)$width <- abs(E(g)$ChangeCoop)*3
E(g)$arrow.size <- abs(E(g)$ChangeCoop)*0


# Figure 4:
set.seed(9999)
pdf('replication/paper/figures/bigPos_new.pdf')
par(mai=c(0,0,1,0)) 
plot(g,
     layout = layout.auto,
     vertex.label.dist=0.8,
     vertex.frame.color='black',
     vertex.label.color='black',
     vertex.label.font=2,
     vertex.label.cex=1)
dev.off()



# FIGURE 5 ---------------------------------------------------------------


g <- graph.data.frame(FinalData[FinalData$ChangeCoop <= -.5,], directed = T)
g = g + vertex("Iran") + vertex("Oman")


# color edges based on positve/negative attribute
#E(g)$color <- ifelse(E(g)$ChangeCoop > 0, "lightblue1", "lightcoral")
E(g)$color <- ifelse(E(g)$ChangeCoop > 0, "dodgerblue", "red")
V(g)$color = "lightcoral"

# change size of nodes, edge, and arrows based on degree
#E(g)$width <- abs(E(g)$ChangeCoop)*3
E(g)$arrow.size <- abs(E(g)$ChangeCoop)*0
V(g)$size = 8


# Figure 5:
set.seed(9999)
par(mai=c(0,0,1,0))
pdf('replication/paper/figures/bigNeg_new.pdf')
plot(g,
     layout = layout.fruchterman.reingold,
     edge.arrow.size=0, 
     vertex.label.cex=0.75,
     vertex.label.dist=0.5,
     #vertex.label.family="Helvetica",
     vertex.label.font=2,
     vertex.shape="circle", 
     #vertex.size=1, 
     vertex.label.color="black", 
     edge.width=0.9)
dev.off()


